﻿using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Blazor.Templates;
using DevExpress.ExpressApp.Templates;
using EasyXaf.EntityDesigner.BusinessObjects;
using EasyXaf.EntityDesigner.BusinessObjects.Entities;
using EasyXaf.EntityDesigner.Editors.DetailViewEditors;

namespace EasyXaf.EntityDesigner.Controllers;

public class PopupWindowController : WindowController
{
    protected override void OnActivated()
    {
        base.OnActivated();
        Frame.TemplateChanged += Frame_TemplateChanged;
    }

    protected override void OnDeactivated()
    {
        Frame.TemplateChanged -= Frame_TemplateChanged;
        base.OnDeactivated();
    }

    private void Frame_TemplateChanged(object sender, EventArgs e)
    {
        if (Frame.Template is IPopupWindowTemplateSize template)
        {
            if (Frame.View.Id == Application.GetDetailViewId(typeof(Entity)))
            {
                template.Width = "1024px";
            }
            else if (Frame.View.Id == Application.GetDetailViewId(typeof(DomainModel)))
            {
                template.Width = "900px";
                ((ISupportActionsToolbarVisibility)template).SetVisible(false);
            }
            else if (Frame.View.Id == Application.GetDetailViewId(typeof(DetailViewObject)))
            {
                template.Width = "90%";
                template.Height = "90%";
                ((ISupportActionsToolbarVisibility)template).SetVisible(false);
            }
            else
            {
                template.Width = "900px";
            }
        }
    }
}
